package gui.utils;

import core.utils.Utils;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

/*Author: John Archer, Contact: john.archer.jpa@gmail.com*/
public class FilePicker extends JPanel implements ActionListener{
    private JButton file;
    private JTextField txt;
    private boolean open;
     
    public FilePicker(String txt, boolean open, int lenTXT, int lenButton) {
        super(new FlowLayout());
        this.setBorder(BorderFactory.createEmptyBorder(0,-4,0,0));
        this.open = open;
        this.add(Utils.getButton(file = new JButton(txt), lenButton, 22, new Color(102, 102, 102), new Color(178, 178, 178), this, txt), BorderLayout.WEST);
        this.txt = new JTextField(lenTXT);
        this.txt.setBackground(Color.WHITE);
        this.txt.setBorder(BorderFactory.createEmptyBorder(3,3,3,3));
        this.txt.setEditable(false);
        this.txt.setFont(Borders.getFont(Borders.GUIFONT));
        this.add(this.txt, BorderLayout.EAST);
    }

    public String getPath(){return txt.getText();}

    public void actionPerformed(ActionEvent evt){
        Object source = evt.getSource();
        if(source == file){
            try{txt.setText(Utils.getFile(new JPanel(), open).getSelectedFile().getPath());}
            catch(Exception e){}
        }
    }
}